import $api from '@/api/index.js'
export default {
	state: {
		pageActive: false,
		activeIndex: 0,
		detail: {},
		param: {
			page: 1,
			sort: ''
		},
		list: {
			data: [],
			last_page: 1,
			current_page: 1
		},
		totalList: [
			{name: '人数', number: 0},
			{name: '访问', number: 0},
			// {name: '免单', number: 0}
		],
		banner: [],
		user_list: [],
		noreadnum:0,
		myhbean:0
	},
	mutations: {
		async updateServiceItem(state, item) {
			let {
				key,
				val
			} = item
			state[key] = val
		}
	},
	actions: {
		async getServiceIndex({
			commit,
			state
		}, param) {
			let d = await $api.service.index(param)
			this.detail = d
			this.banner = this.detail.banner
			// debugger
			this.user_list = this.detail.user_list
			this.noreadnum = this.detail.noreadnum
			uni.setStorageSync('noreadnum',this.detail.noreadnum)
			this.myhbean = this.detail.total_hbean
			this.totalList = []
			this.totalList.push({name: '人数', number: this.detail.data_statistics.user_num})
			this.totalList.push({name: '访问', number: this.detail.data_statistics.traffic_volume})
			// this.totalList.push({name: '免单', number: this.detail.data_statistics.free_payment}) this.detail.data_statistics.user_num
			
			commit('updateServiceItem', {
				key: 'detail',
				val: this.detail
			})
			commit('updateServiceItem', {
				key: 'user_list',
				val: this.user_list
			})
			commit('updateServiceItem', {
				key: 'banner',
				val: this.banner
			})
			commit('updateServiceItem', {
				key: 'totalList',
				val: this.totalList
			})
			commit('updateServiceItem', {
				key: 'noreadnum',
				val: this.noreadnum
			})
		},
		// 服务
		async getServiceList({
			commit,
			state
		}, param) {
			let d = await $api.service.getServiceList(param)
			let oldList = state.list
			let newList = d
			let list = {}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 爱我，我爱
		async getUserList({
			commit,
			state
		}, param) {
			let d = await $api.service.getUserList(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 我收藏的用户
		async getMycollectuser({
			commit,
			state
		}, param) {
			let d = await $api.service.getMycollectuser(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		
		// 我收藏的老师
		async mylikeTeacher({
			commit,
			state
		}, param) {
			let d = await $api.service.mylikeTeacher(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 我收藏的服务
		async mylikeCoach({
			commit,
			state
		}, param) {
			let d = await $api.service.mylikeCoach(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 老师
		async getTeacherList({
			commit,
			state
		}, param) {
			let d = await $api.service.getTeacherList(param)
			let oldList = state.list
			let newList = d
			let list = {}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 店铺
		async getShopList({
			commit,
			state
		}, param) {
			let d = await $api.service.getShopList(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			list.data.forEach((item, index) => {
				item.title = ''
				item.item.forEach((item2) => {
					item.title += item2.title + '   '
				})
			})
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 我收藏的门店
		async myLikeshop({
			commit,
			state
		}, param) {
			let d = await $api.service.myLikeshop(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			list.data.forEach((item, index) => {
				item.title = ''
				item.item.forEach((item2) => {
					item.title += item2.title + '   '
				})
			})
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		// 免单
		async getFreeList({
			commit,
			state
		}, param) {
			let d = await $api.service.getFreeList(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		},
		
		// 我收藏的乐购
		async myLikegoods({
			commit,
			state
		}, param) {
			let d = await $api.service.myLikegoods(param)
			let oldList = state.list
			let newList = {
				data: d,
				last_page: 1,
				current_page: 1
			}
			let list = {
				data: [],
				last_page: 1,
				current_page: 1
			}
			if (param.page == 1) {
				list = newList
			} else {
				newList.data = oldList.data.concat(newList.data)
				list = newList
			}
			commit('updateServiceItem', {
				key: 'list',
				val: list
			})
		}
	},
}
